﻿
Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_News_HomeNews
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadData()
        End If
    End Sub
    Private Sub LoadData()
        Dim _ParentId As Integer = ConfigurationManager.AppSettings("RootNews")
        Using Data As New VSEntities
            Dim p = (From q In Data.Menus Where q.ParentId = _ParentId And q.MenuPosition = 1 Order By q.Sort Ascending Select q.MenuId, q.Title, q.Description, q.QueryString).ToList
            rptCate.DataSource = p
            rptCate.DataBind()
        End Using
    End Sub
    Protected Sub rptCate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCate.ItemDataBound
        Dim row = CType(e.Item.DataItem, Object)
        Dim rptSub As Repeater = CType(e.Item.FindControl("rptSub"), Repeater)
        Dim hplCate As HyperLink = CType(e.Item.FindControl("hplCate"), HyperLink)
        Dim hplMore As HyperLink = CType(e.Item.FindControl("hplMore"), HyperLink)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)

        hplMore.NavigateUrl = MainDomain & "tin-oto/" & row.QueryString & "-" & row.MenuId.ToString & ".html"
        Dim intMenuId As Integer = row.MenuId.ToString
        hplCate.Text = row.Title
        hplCate.NavigateUrl = MainDomain & "tin-oto/" & row.QueryString & "-" & row.MenuId.ToString & ".html"
        hplCate.Attributes.Add("style", "cursor:pointer")
        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        Dim lblDate As Label = CType(e.Item.FindControl("lblDate"), Label)
        Dim ltrDesc As Literal = CType(e.Item.FindControl("ltrDesc"), Literal)

        Using Data As New VSEntities
            Dim h = (From q In Data.Contents Where q.MenuId = intMenuId And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending Select q.Title, q.QueryString, q.Id, q.Image, q.Description, q.PublishedDate).FirstOrDefault
            If Not h Is Nothing Then
                hplTitle.Text = h.Title
                hplTitle.NavigateUrl = MainDomain & "chi-tiet/" & h.QueryString & "-" & h.Id.ToString & ".html"
                ltrDesc.Text = h.Description
                lblDate.Text = DateToString(h.PublishedDate, "mm/dd/yyyy")
                img.ImageUrl = CookedImageURL("240-180", h.Image)
                'Lay toan bo cap con cua Cate
                Dim a = (From r In Data.uspSelectSubMenu(intMenuId) Select r.MenuId).ToArray
                Dim p = (From q In Data.Contents Where q.Id <> h.Id And (q.MenuId = intMenuId Or a.Contains(q.MenuId)) And q.StatusId = 3 And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) Order By q.PublishedDate Descending Select q.Title, q.QueryString, q.Id, q.Image).Take(5).ToList
                rptSub.DataSource = p
                rptSub.DataBind()
            End If

        End Using
    End Sub
    Protected Sub rptSub_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim row = CType(e.Item.DataItem, Object)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & "chi-tiet/" & row.QueryString & "-" & row.Id.ToString & ".html"
    End Sub

End Class
